home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / LOCALM~1 / Settings.bas < prev    next >
BASIC Source File  |  1997-06-14  |  6KB  |  180 lines

  1. Attribute VB_Name = "MSettings"
  2. Option Explicit
  3.  
  4. Public Enum EErrorSettings
  5.     eeBaseSettings = 13600  ' Settings
  6. End Enum
  7.  
  8. Const sSep = ","
  9. Const sWinSection = "Window List"
  10. Const sCtlSection = "Control List"
  11.  
  12. Sub RestoreWindow(ByVal hWnd, sTitle As String)
  13.     Dim sValue As String, f As Integer
  14.  
  15.     sValue = GetSetting(App.ExeName, sWinSection, sTitle, sEmpty)
  16.     ' Quit if no entry
  17.     If sValue = sEmpty Then Exit Sub
  18.  
  19.     Dim wp As WINDOWPLACEMENT, iPos As Integer
  20.     wp.showCmd = Val(MParse.GetToken(sValue, sSep))
  21.     If IsWindowVisible(hWnd) = False Then
  22.         wp.showCmd = SW_HIDE
  23.     End If
  24.     ' wp.Flags = 0 (no flags required)
  25.     wp.ptMinPosition.x = Val(MParse.GetToken(sEmpty, sSep))
  26.     wp.ptMinPosition.y = Val(MParse.GetToken(sEmpty, sSep))
  27.     wp.ptMaxPosition.x = Val(MParse.GetToken(sEmpty, sSep))
  28.     wp.ptMaxPosition.y = Val(MParse.GetToken(sEmpty, sSep))
  29.     wp.rcNormalPosition.Left = Val(MParse.GetToken(sEmpty, sSep))
  30.     wp.rcNormalPosition.Top = Val(MParse.GetToken(sEmpty, sSep))
  31.     wp.rcNormalPosition.Right = Val(MParse.GetToken(sEmpty, sSep))
  32.     wp.rcNormalPosition.bottom = Val(MParse.GetToken(sEmpty, sSep))
  33.     ' Remember to set length
  34.     wp.length = Len(wp)
  35.     ' Send all your settings to the system
  36.     f = SetWindowPlacement(hWnd, wp)
  37.  
  38. End Sub
  39.  
  40. Sub SaveWindow(ByVal hWnd, sTitle As String)
  41.     
  42.     Dim sValue As String, f As Boolean
  43.     Dim wp As WINDOWPLACEMENT
  44.     ' First set type length for Windows
  45.     wp.length = Len(wp)
  46.     ' Get coordinates and other data about the window
  47.     f = GetWindowPlacement(hWnd, wp)
  48.     ' Read and use the data
  49.     sValue = wp.showCmd & ","
  50.     sValue = sValue & wp.ptMinPosition.x & ","
  51.     sValue = sValue & wp.ptMinPosition.y & ","
  52.     sValue = sValue & wp.ptMaxPosition.x & ","
  53.     sValue = sValue & wp.ptMaxPosition.y & ","
  54.     sValue = sValue & wp.rcNormalPosition.Left & ","
  55.     sValue = sValue & wp.rcNormalPosition.Top & ","
  56.     sValue = sValue & wp.rcNormalPosition.Right & ","
  57.     sValue = sValue & wp.rcNormalPosition.bottom
  58.     
  59.     SaveSetting App.ExeName, sWinSection, sTitle, sValue
  60.  
  61. End Sub
  62.  
  63. Sub ClearWindowSetting(sTitle As String)
  64.     DeleteSetting App.ExeName, sWinSection, sTitle
  65. End Sub
  66.  
  67. ' These could have been implemented with Form properties, but
  68. ' API does a better job of handling minimized and maximized windows.
  69.  
  70. ' Call only in Form_Load before Show
  71. #If fComponent Then
  72. Sub RestoreForm(frm As Object, Optional sTitle As String)
  73. #Else
  74. Sub RestoreForm(frm As Form, Optional sTitle As String)
  75. #End If
  76.     If sTitle = sEmpty Then sTitle = frm.Caption
  77.     RestoreWindow frm.hWnd, sTitle
  78. End Sub
  79.  
  80. ' Call only in Form_Unload when form is closing
  81. #If fComponent Then
  82. Sub SaveForm(frm As Object, Optional sTitle As String)
  83. #Else
  84. Sub SaveForm(frm As Form, Optional sTitle As String)
  85. #End If
  86.     If sTitle = sEmpty Then sTitle = frm.Caption
  87.     SaveWindow frm.hWnd, sTitle
  88. End Sub
  89.  
  90. #If fComponent Then
  91. Sub ClearFormSetting(frm As Object, Optional sTitle As String)
  92. #Else
  93. Sub ClearFormSetting(frm As Form, Optional sTitle As String)
  94. #End If
  95.     If sTitle = sEmpty Then sTitle = frm.Caption
  96.     DeleteSetting App.ExeName, sWinSection, sTitle
  97. End Sub
  98.  
  99. #If fComponent Then
  100. Sub RestoreCtl(ctl As Object, sTitle As String)
  101. #Else
  102. Sub RestoreCtl(ctl As Control, sTitle As String)
  103. #End If
  104. With ctl
  105.     Dim sValue As String, s As String, i As Long
  106.     sValue = GetSetting(App.ExeName, sCtlSection, sTitle, sEmpty)
  107.     ' Quit if no entry
  108.     If sValue = sEmpty Then Exit Sub
  109.  
  110.     ' Set left and top while in normal mode
  111.     On Error Resume Next
  112.     .Left = MParse.GetToken(sValue, sSep)
  113.     .Top = MParse.GetToken(sEmpty, sSep)
  114.     .Width = MParse.GetToken(sEmpty, sSep)
  115.     .Height = MParse.GetToken(sEmpty, sSep)
  116.     .Enabled = MParse.GetToken(sEmpty, sSep)
  117.     .Visible = MParse.GetToken(sEmpty, sSep)
  118.     s = .Caption
  119.     If Err = 0 Then .Caption = MParse.GetToken(sEmpty, sSep)
  120.     i = .BackColor
  121.     If Err = 0 Then .BackColor = MParse.GetToken(sEmpty, sSep)
  122.     i = .ForeColor
  123.     If Err = 0 Then .ForeColor = MParse.GetToken(sEmpty, sSep)
  124.     ' Enhance to restore anything else you saved
  125.  
  126. End With
  127. End Sub
  128.  
  129. #If fComponent Then
  130. Sub SaveCtl(ctl As Object, sTitle As String)
  131. #Else
  132. Sub SaveCtl(ctl As Control, sTitle As String)
  133. #End If
  134. With ctl
  135.     Dim sValue As String, s As String, i As Long
  136.  
  137.     On Error Resume Next
  138.     sValue = sValue & .Left & "," & .Top & ","
  139.     sValue = sValue & .Width & "," & .Height & ","
  140.     sValue = sValue & .Enabled & ","
  141.     sValue = sValue & .Visible & ","
  142.     s = .Caption
  143.     If Err = 0 Then sValue = sValue & .Caption & ","
  144.     i = .BackColor
  145.     If Err = 0 Then sValue = sValue & .BackColor & ","
  146.     i = .ForeColor
  147.     If Err = 0 Then sValue = sValue & .ForeColor & ","
  148.     
  149.     ' Enhance to save anything else you need
  150.  
  151.     SaveSetting App.ExeName, sCtlSection, sTitle, sValue
  152.  
  153. End With
  154. End Sub
  155.  
  156. ' Add more save and restore functions:
  157. '     SaveFont/RestoreFont
  158. '     SaveTextBox/RestoreTextBox
  159.  
  160. #If fComponent = 0 Then
  161. Private Sub ErrRaise(e As Long)
  162.     Dim sText As String, sSource As String
  163.     If e > 1000 Then
  164.         sSource = App.ExeName & ".Settings"
  165.         Select Case e
  166.         Case eeBaseSettings
  167.             BugAssert True
  168.        ' Case ee...
  169.        '     Add additional errors
  170.         End Select
  171.         Err.Raise COMError(e), sSource, sText
  172.     Else
  173.         ' Raise standard Visual Basic error
  174.         sSource = App.ExeName & ".VBError"
  175.         Err.Raise e, sSource
  176.     End If
  177. End Sub
  178. #End If
  179.  
  180.